package com.controller.secretary;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

public class report extends Controller{

	private int page;
	private int rows;
	private String sort;
	private String order;
	private String id;
	
	@ActionKey("/getDPMIInfo")
	public void getDPMIInfo() {
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "departmentissue");
		//操作的列+别名
		//学生姓名
		config.put("col_notice", "departissue_title");
		config.put("alias_noticee", "notice");
		//论文名
		config.put("col_subPerson", "departissue_man");
		config.put("alias_subPerson", "subPerson");
		//论文当前状态
		config.put("col_time", "departissue_issuetime");
		config.put("alias_time", "time");
		//论文id
		config.put("col_URL", "departissue_attachment");
		config.put("alias_URL", "URL");
		//教师姓名
		config.put("col_content", "departissue_content");
		config.put("alias_content", "content");
		//论文存储地址
		config.put("col_id", "departissue_id");
		config.put("alias_id", "id");
		
		
		//-----------获取前台传过来的参数，用于分页获取数据--------------
		page = getParaToInt("page");
		rows = getParaToInt("rows");
		sort = getPara("sort");
		order = getPara("order");

		
		
		//--------------基础sql语句,使用占位符占位，在之后再替换-------------
		String sql = " from %s where 1=1";
		String select="select %s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s";
		
		
		//---------------添加分页属性-----------------
		if (sort != null && order != null)
			sql = sql + " order by " + sort + " " + order;
		
		//----------------替换----------------------
		sql=String.format(sql, config.get("tableName"));
		select=String.format(select, 
				config.get("col_id"),config.get("alias_id"),
				config.get("col_content"),config.get("alias_content"),
				config.get("col_URL"),config.get("alias_URL"),
				config.get("col_time"),config.get("alias_time"),
				config.get("col_subPerson"),config.get("alias_subPerson"),
				config.get("col_notice"),config.get("alias_noticee"));
		
		
		//----------------获取分页属性，传志前台-------------------
		Page<Record> stu=Db.paginate(page, rows, select, sql);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", stu.getTotalRow());
		m.put("rows", stu.getList());
		renderJson(m);
	}
	
	@ActionKey("/DPMIUpload")
	public void upload() throws UnsupportedEncodingException
	{
		String s=getClass().getResource("/").getFile().toString();
		s=s.substring(0, s.lastIndexOf("/WEB-INF"));
		s=URLDecoder.decode(s,"utf-8");
		
		UploadFile uf=getFile("file", s+"/WEB-INF/uploadDir/");
		File file=new File(uf.getSaveDirectory()+uf.getOriginalFileName());
		file.delete();
		
		
		String sql="update %s set %s='%s' where %s=%s";
		sql=String.format(sql, "departmentissue",
				"departissue_attachment",uf.getFileName(),
				"departissue_id",getPara("author"));
		Db.update(sql);
		renderJson("{\"success\":true}");
	}
	
	@ActionKey("/DPMIdel")
	public void DPMIdel()
	{
		id=getPara("id");
		System.out.println(id);
		
		String sql="delete from %s where %s='%s'";
		sql=String.format(sql, "departmentissue",
				"departissue_id",id);
		Db.update(sql);
		renderJson("{\"success\":true}");
	}
	
	@ActionKey("/DPMIadd")
	public void DPMIadd()
	{
		String title=getPara("title");
		String content=getPara("content");
		//String inscribe=getPara("inscribe");
		String inscribe=(String) getSession().getAttribute("teacher_id");//获取了发布人的id
		String data=getPara("data");
		System.out.println(title);
		System.out.println(content);
		System.out.println(inscribe);
		System.out.println(data);
		String sql="insert into %s(%s,%s,%s,%s) values('%s','%s','%s','%s')";
		sql=String.format(sql, "departmentissue",
				"departissue_title","departissue_content","departissue_man","departissue_issuetime",
				title,content,inscribe,data);
		Db.update(sql);
	}
	
	@ActionKey("/sct2change")
	public void sct2change()
	{
		String thsisId=getPara("id");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		
		String baseSql="insert into %s(%s,%s,%s) values('%s','%s','%s')";
		baseSql=String.format(baseSql, "excellentthesisissue",
				"excllentthesisissue_thesistitle","excllentthesisissue_man","excllentthesisissue_issuetime",
				"%s",getSession().getAttribute("teacher_id"),df.format(new Date()));
		//System.out.println(baseSql);
		String[] id=thsisId.split(",");
		String sql="";
		for(int i=0;i<id.length;i++)
		{
			sql=String.format(baseSql, id[i]);
			sql=sql+";";
		}
		Db.update(sql);
		renderJson("{\"success\":true}");
		//System.out.println(sql);
		//	System.out.println(id[i]);
	}
	@ActionKey("/sct2del")
	public void sct2del()
	{
		id=getPara("id");
		System.out.println(id);
		
		String sql="delete from %s where %s='%s'";
		sql=String.format(sql, "excellentthesisissue",
				"excllentthesisissue_thesistitle",id);
		Db.update(sql);
		renderJson("{\"success\":true}");
	}
	@ActionKey("/getsct2_1Info")
	public void getsct2_1Info() {
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "sct2_1");
		//操作的列+别名
		//学生姓名
		config.put("col_stuName", "student_name");
		config.put("alias_stuName", "stuName");
		//论文名
		config.put("col_stuNO", "student_no");
		config.put("alias_stuNO", "stuNO");
		//论文当前状态
		config.put("col_tchName", "teacher_name");
		config.put("alias_tchName", "tchName");
		//论文id
		config.put("col_thesis", "thesistitle_id");
		config.put("alias_thesis", "thesis");
		//教师姓名
		config.put("col_thesisName", "thesistitle_name");
		config.put("alias_thesisName", "thesisName");
		
		
		//-----------获取前台传过来的参数，用于分页获取数据--------------
		page = getParaToInt("page");
		rows = getParaToInt("rows");
		sort = getPara("sort");
		order = getPara("order");

		
		
		//--------------基础sql语句,使用占位符占位，在之后再替换-------------
		String sql = " from %s where (1=1)";
		sql=sql+" and (fthesis_guidtchescore='A' or fthesis_judgetchscore='A' or replyrecord_score='A')";
		String select="select %s as %s,%s as %s,%s as %s,%s as %s,%s as %s";
		
		
		//---------------添加分页属性-----------------
		if (sort != null && order != null)
			sql = sql + " order by " + sort + " " + order;
		
		//----------------替换----------------------
		sql=String.format(sql, config.get("tableName"));
		select=String.format(select, 
				config.get("col_stuName"),config.get("alias_stuName"),
				config.get("col_stuNO"),config.get("alias_stuNO"),
				config.get("col_tchName"),config.get("alias_tchName"),
				config.get("col_thesis"),config.get("alias_thesis"),
				config.get("col_thesisName"),config.get("alias_thesisName"));
		
		
		//----------------获取分页属性，传志前台-------------------
		Page<Record> stu=Db.paginate(page, rows, select, sql);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", stu.getTotalRow());
		m.put("rows", stu.getList());
		renderJson(m);
	}
	@ActionKey("/getsct2_2Info")
	public void getsct2_2Info() {
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "sct2_2");
		//操作的列+别名
		//学生姓名
		config.put("col_stuName", "student_name");
		config.put("alias_stuName", "stuName");
		//论文名
		config.put("col_stuNO", "student_no");
		config.put("alias_stuNO", "stuNO");
		//论文当前状态
		config.put("col_tchName", "teacher_name");
		config.put("alias_tchName", "tchName");
		//论文id
		config.put("col_thesis", "thesistitle_id");
		config.put("alias_thesis", "thesis");
		//教师姓名
		config.put("col_thesisName", "thesistitle_name");
		config.put("alias_thesisName", "thesisName");
		//教师姓名
		config.put("col_man", "excllentthesisissue_man");
		config.put("alias_man", "man");
		//教师姓名
		config.put("col_issuetime", "excllentthesisissue_issuetime");
		config.put("alias_issuetime", "issuetime");
		
		
		//-----------获取前台传过来的参数，用于分页获取数据--------------
		page = getParaToInt("page");
		rows = getParaToInt("rows");
		sort = getPara("sort");
		order = getPara("order");

		
		
		//--------------基础sql语句,使用占位符占位，在之后再替换-------------
		String sql = " from %s where 1=1";
		String select="select %s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s";
		
		
		//---------------添加分页属性-----------------
		if (sort != null && order != null)
			sql = sql + " order by " + sort + " " + order;
		
		//----------------替换----------------------
		sql=String.format(sql, config.get("tableName"));
		select=String.format(select, 
				config.get("col_stuName"),config.get("alias_stuName"),
				config.get("col_stuNO"),config.get("alias_stuNO"),
				config.get("col_tchName"),config.get("alias_tchName"),
				config.get("col_thesis"),config.get("alias_thesis"),
				config.get("col_thesisName"),config.get("alias_thesisName"),
				config.get("col_man"),config.get("alias_man"),
				config.get("col_issuetime"),config.get("alias_issuetime"));
		
		
		//----------------获取分页属性，传志前台-------------------
		Page<Record> stu=Db.paginate(page, rows, select, sql);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", stu.getTotalRow());
		m.put("rows", stu.getList());
		renderJson(m);
	}
}
